﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class XArc3PtJig : XUiJigBase
	{
		public XArc3PtJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = XArc3PtJig.EStage.eNoOper;
		}

		public XYZ PickStartPoint(string strPrompt = "请确定圆弧起点：")
		{
			try
			{
				this.JigStage = XArc3PtJig.EStage.eStartPt;
				this.StartPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.StartPoint = null;
			}
			this.JigStage = XArc3PtJig.EStage.eNoOper;
			return this.StartPoint;
		}

		public XYZ PickEndPoint(XYZ ptStart, string strPrompt = "请确定圆弧终点：")
		{
			try
			{
				this.JigStage = XArc3PtJig.EStage.eEndPt;
				this.StartPoint = ptStart;
				this.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, this.EndPoint, this.StartPoint);
				bool flag = axisPoint != null;
				if (flag)
				{
					this.EndPoint = axisPoint;
				}
			}
			catch
			{
				this.EndPoint = null;
			}
			this.JigStage = XArc3PtJig.EStage.eNoOper;
			return this.EndPoint;
		}

		public XYZ PickOnCurvePoint(XYZ ptStart, XYZ ptEnd, string strPrompt = "请确定圆弧上的点：")
		{
			try
			{
				this.JigStage = XArc3PtJig.EStage.eOnCurvePt;
				this.StartPoint = ptStart;
				this.EndPoint = ptEnd;
				this.OnCurvePoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.OnCurvePoint = null;
			}
			this.JigStage = XArc3PtJig.EStage.eNoOper;
			return this.OnCurvePoint;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			bool flag = this.JigStage == XArc3PtJig.EStage.eEndPt;
			if (flag)
			{
                System.Drawing.Point ptStart = base.Revit2Client(this.StartPoint);
				Graphics graphics = paintEventArgs.Graphics;
				XYZ ptUser = base.Client2Revit(this.m_ptCur);
				XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, ptUser, this.StartPoint);
				bool flag2 = axisPoint != null;
				if (flag2)
				{
					this.m_ptCur = base.Revit2Client(axisPoint);
					base.DrawAxisLine(graphics, this.StartPoint, axisPoint);
				}
				XLine2D xline2D = new XLine2D(ptStart, this.m_ptCur);
				XGraphics g = new XGraphics(graphics, base.JigPen);
				xline2D.Draw(g);
			}
			else
			{
				bool flag3 = this.JigStage == XArc3PtJig.EStage.eOnCurvePt;
				if (flag3)
				{
                    System.Drawing.Point ptStart2 = base.Revit2Client(this.StartPoint);
                    System.Drawing.Point ptEnd = base.Revit2Client(this.EndPoint);
                    System.Drawing.Point ptCur = this.m_ptCur;
					Graphics graphics2 = paintEventArgs.Graphics;
					XArc2D xarc2D = new XArc2D(ptStart2, ptEnd, ptCur);
					XGraphics g2 = new XGraphics(graphics2, base.JigPen);
					xarc2D.Draw(g2);
				}
			}
		}

		public override List<Curve> CreateCurves()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				bool flag = this.StartPoint != null && this.EndPoint != null && this.OnCurvePoint != null;
				if (flag)
				{
					Arc item = Arc.Create(this.StartPoint, this.EndPoint, this.OnCurvePoint);
					list.Add(item);
				}
			}
			catch
			{
			}
			return list;
		}

		public XYZ StartPoint { get; set; }

		public XYZ EndPoint { get; set; }

		public XYZ OnCurvePoint { get; set; }

		public XArc3PtJig.EStage JigStage { get; set; }

		public enum EStage
		{
			eNoOper,
			eStartPt,
			eEndPt,
			eOnCurvePt
		}
	}
}
